var app = getApp();
var Interval;
Page({
  onLoad(e){
    this.setData({e:e});
    // e.from = 'share';
    // e.user_bargain_id = '12';
    if(e.from==="list"){//从公共列表进入，未创建单
      this.setData({ bargain_id: e.bargain_id,"from":"list"});
      this.detail();
    }  
    else if(e.from==="my"){
      this.setData({ user_bargain_id: e.user_bargain_id});
      this.detail2();
    }
    else if (e.from === "share"){
      this.setData({ user_bargain_id: e.user_bargain_id});
      this.detail2();
    }
    let pages = getCurrentPages();//页面栈
    if (pages.length === 1) {//分享进入
      this.setData({ xcxpages1:true })
    }
  },
  onShow(){
    if(this.data.onshow==1&&this.data.from!="list"){//刷新页面
      this.onLoad(this.data.e);
    }
    this.setData({ onshow:1});
  },
  data:{
    onshow:0
  },
  onShareAppMessage(){
    return {
      title: this.data.items.title,
      imageUrl:'https://m.zujiekeji.cn/xcximg/xcxkanjia/kanjiashare.png',
      path: '/pages/kanjia/detail/detail?user_bargain_id=' + this.data.user_bargain_id+'&from=share',
    }
  },
  detail(){//未开单
    let json  = {
      user_id : wx.getStorageSync('userdata').user_id || "",
      token: wx.getStorageSync('token'),
      bargain_id: this.data.bargain_id
    }
    app.request('book/bargain/bargain_initiate','POST',json,'detailb',this)
  },
  detailb(data){
    if(data.code!=200){
      app.tishi('提示',data.msg);
      return false
    }
    let daojishi = data.data.surplus_time >= 0 ? data.data.surplus_time : 0;
    this.setData({
      items: data.data, daojishi: daojishi,
      yuanjia: parseFloat(data.data.old_price),
      dijia: parseFloat(data.data.floor_price) || 0,
      card: data.data.card
    });
    if (daojishi > 0 && this.data.startjishi != 1) {
      this.setData({ startjishi: 1 });
      this.daojishi();
    }
  },
  detail2(){//从我的记录里来
    let json = {
      user_id : wx.getStorageSync('userdata').user_id || "",
      token: wx.getStorageSync('token') || "",
      user_bargain_id: this.data.user_bargain_id
    }
    app.request('book/bargain/bargain_link', 'POST', json,'detail2b',this);
  },
  detail2b(data) {//从我的记录里来 回调
    if (data.code != 200) {
      app.tishi('提示', data.msg);
      return false
    }
    if (data.data.judge.is_owner==1){
      this.setData({"from":"my"})
    }else{
      this.setData({ "from": "share" })
    }
    let show = data.data.bargain_show;//展示信息
    let daojishi = show.surplus_time >= 0 ? show.surplus_time : 0;
    this.setData({ items: show, daojishi: daojishi,
      yuanjia: parseFloat(show.old_price), dangqianjia: parseFloat(show.price)||0,
      dijia: parseFloat(show.floor_price) || 0,
    })
    if (daojishi > 0 && this.data.startjishi!=1){
      this.setData({startjishi:1});
      this.daojishi();
    }else{
      this.setData({day:0,hours:0,minute:0,time:0});
    }
    let hot_bargain = data.data.hot_bargain;
    for (let i = 0; i < hot_bargain.length; i++) {
      hot_bargain[i].floor_price = parseInt(hot_bargain[i].floor_price)
    }
    console.log(data.data.judge);
    this.setData({ bangkanlist: data.data.bargain_aid, judge: data.data.judge,
      hot_bargain: hot_bargain, card: data.data.card})//帮砍列表,推荐
  },

  daojishi(){//倒计时
    let _this=this;
    Interval = setInterval(()=>{
      if (_this.data.daojishi<=0){
        clearInterval(Interval);
      }
      _this.setData({
        day: Math.floor(_this.data.daojishi / 3600 / 24),
        hours: Math.floor((_this.data.daojishi%(3600*24))/3600),
        minute: Math.floor((_this.data.daojishi % (3600)) / 60),
        time: Math.floor(_this.data.daojishi % (60)),
        daojishi:_this.data.daojishi-1
      })
    },1000)
  },
  kanjia(){//发起新单
    let token = wx.getStorageSync('token');
    if(token === ""){
      wx.navigateTo({url:'/pages/login/login'});
      return false
    }
    let json = {
      token: token,
      user_id:wx.getStorageSync('userdata').user_id,
      bargain_id: this.data.bargain_id
    }
    app.request('book/bargain/create_user_bargain', 'POST', json,'kanjiab',this)
  },
  kanjiab(data){//发起新单回调
    if(data.code==400){
      wx.showModal({
        title: '提示',
        content: data.msg,
        showCancel:false,
        success(res){
          wx.redirectTo({ url: '/pages/kanjia/kanjiarecord/kanjiarecord' })
        }
      })
    } else if (data.code == 200){
      wx.showToast({
        title: data.msg, icon: 'success', duration: 800, mask: true
      })
      setTimeout(() => {
        wx.redirectTo({ url: '/pages/kanjia/kanjiarecord/kanjiarecord' })
      }, 800)
    } else{
      app.tishi('提示',data.msg);
    }
    
  },
  share(){//分享按钮
    this.setData({ share:true});
  },
  closeshare() {
    this.setData({ share: false})
  },
  tuijianwei(e){//推荐列表
    let bargain_id = e.currentTarget.id;
    wx.redirectTo({ url: '/pages/kanjia/detail/detail?bargain_id=' + bargain_id + '&from=list' })
  },
  getPhoneNumber(e) {//帮朋友砍价
    let token = wx.getStorageSync('token');
    if(token === ""){//没登陆的
      if (!e.detail.iv) {//点了拒绝，无操作
        return false;
      }
      let json = {
        iv: e.detail.iv, encryptedData: e.detail.encryptedData,
        session_key: wx.getStorageSync('session_key')
      }
      app.request('index/user/show_user_phone', 'POST', json, 'getPhoneNumberb', this);
    }else{//已登陆的
      this.bangkanjia();//帮砍价
    }
  },
  getPhoneNumberb(data) {//获取手机号回调
    let json = {
       user_phone : JSON.parse(data.data).phoneNumber,
       openid : wx.getStorageSync('openid')
    }
    app.request('index/user/wx_accredit_login','POST',json,'denglub',this);
  },
  denglub(data){//登录||注册并登录
    if (data.code != 200) {
      app.tishi('提示', data.msg);
      return false
    }
    wx.setStorageSync('userdata', data.data);
    wx.setStorageSync('token', data.data.token);
    this.detail2();//刷新
    this.bangkanjia();//登录成功，帮砍价
  },
  bangkanjia(){//点击帮砍价
    let json = {
      token: wx.getStorageSync('token'),
      user_id: wx.getStorageSync('userdata').user_id,
      user_bargain_id: this.data.user_bargain_id
    }
    app.request('book/bargain/let_us_bargain', 'POST', json, 'bangkanjiab', this);
  },
  bangkanjiab(data){//帮砍回调
    if(data.code!=200){
      app.tishi('提示', data.msg);
      return false;
    }
    this.setData({ kanchenggong : true,kanprice:data.data.price});
    this.detail2();//刷新
    let _this = this;
    setTimeout(()=>{
      _this.setData({ kanchenggong: false });
    },1000)
  },
  goshouye() {
    wx.reLaunch({
      url: '/pages/shouye/shouye',
    })
  },
  buy() {//优惠||底价买
    let card = this.data.card;
    card.price = this.data.dangqianjia ;
    card.olddeposit = card.deposit;
    wx.setStorageSync('buycard', card);
    wx.navigateTo({ url: '/pages/home/paycard/paycard?from=canjia&user_bargain_id=' + this.data.user_bargain_id,})
  },
  yuanjiabuy() {//原价买
    let card = this.data.card;
    card.olddeposit = card.deposit;
    wx.setStorageSync('buycard', card);
    wx.navigateTo({ url: '/pages/home/paycard/paycard?from=canjia'})
  },
  savephoto() {
    wx.navigateTo({
      url: '/pages/group/savephoto/savephoto?from=kanjia&id=' + this.data.user_bargain_id,
    })
  },
  wanfa(){
    wx.navigateTo({url:'/pages/kanjia/wanfa/wanfa'})
  }
})